(function() {
  $(function() {
    var field, form, trcode, trctl, _i, _j, _len, _len1, _ref, _ref1;
    form = $('form.main');
    form.form({
      inline: true,
      fields: {
        name: 'empty'
      }
    });
    form.find('.ui.button#trcode_all').click(function() {
      return form.find('.checkbox.trcode').each(function() {
        return $(this).checkbox('check');
      });
    });
    form.find('.ui.button#trcode_none').click(function() {
      return form.find('.checkbox.trcode').each(function() {
        return $(this).checkbox('uncheck');
      });
    });
    trctl = form.form('get value', 'trctl');
    if (trctl.trim().length > 0) {
      trctl = JSON.parse(trctl);
      form.find('.checkbox.trcode').checkbox('uncheck');
      $.each(trctl.trcodes, function(k, v) {
        return form.find(".checkbox.trcode." + k).checkbox('check');
      });
      form.form('set value', 'trctl', '');
      _ref = ['t20', 't60', 'tz0'];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        trcode = _ref[_i];
        if (!trctl[trcode]) {
          continue;
        }
        _ref1 = window.trctl_fields;
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          field = _ref1[_j];
          if (trctl[trcode][field]) {
            form.find(".fields.trctl." + trcode + " input[name=" + field + "]").val(trctl[trcode][field]);
          }
        }
      }
    }
    return form.submit(function(evt) {
      var val, _k, _l, _len2, _len3, _ref2, _ref3;
      if (!$(this).form('is valid')) {
        return evt.preventDefault();
      }
      trctl = {};
      trctl.trcodes = {};
      form.find('.checkbox.trcode').each(function() {
        if ($(this).checkbox('is checked')) {
          return trctl.trcodes[$(this).find('input').attr('name')] = true;
        }
      });
      _ref2 = ['t20', 't60', 'tz0'];
      for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
        trcode = _ref2[_k];
        trctl[trcode] = {};
        _ref3 = window.trctl_fields;
        for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
          field = _ref3[_l];
          val = form.find(".fields.trctl." + trcode + " input[name=" + field + "]").val();
          if (val && val.trim().length > 0) {
            trctl[trcode][field] = val;
          }
        }
      }
      return form.form('set value', 'trctl', JSON.stringify(trctl));
    });
  });

}).call(this);
